
package edu.xlh.travel.controller;

import edu.xlh.travel.common.Result;
import edu.xlh.travel.service.UserService;
import edu.xlh.travel.service.ValidateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;;
import javax.servlet.http.HttpServletRequest;



@Api(tags = "通过邮件修改密码相关api")
@RestController
@RequestMapping(value = "/validate")
public class ValidateController {

    @Autowired
    private ValidateService validateService;

    @Autowired
    private UserService userService;



/**
     * 发送忘记密码邮件请求，每日申请次数不超过20次，每次申请间隔不低于1分钟
     * @param email
     * @param request
     * @return
     */

    @ApiOperation(value = "发送忘记密码邮件", notes = "发送忘记密码邮件")
    @RequestMapping(value = "/sendValidationEmail", method = {RequestMethod.POST})
    public Result sendValidationEmail(@ApiParam("邮箱地址") @RequestParam("email") String email,
                                      HttpServletRequest request) throws MessagingException {
        return validateService.sendValidationEmail(email , request);
    }


/**
     * 将url的token和数据库里的token匹配，成功后便可修改密码，token有效期为5分钟
     * @param token
     * @param password
     * @param confirmPassword
     * @return
     */

    @ApiOperation(value = "重置密码,邮箱中的token有效时间为5分钟,每天每个用户最多发10次邮件", notes = "重置密码")
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    public Result resetPassword(@ApiParam("token") @RequestParam("token") String token,
                                    @ApiParam("密码") @RequestParam("password") String password,
                                    @ApiParam("密码确认") @RequestParam("confirmPassword") String confirmPassword){

        return validateService.resetPassword(token , password , confirmPassword);
    }
}


